package com.china08.yxyapi.api.manage.sch.platform.resource;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.service.yxyresource.YxyResourceServicePointService;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 学校局平台办公 资源管理 审核
 * 
 */
@CrossOrigin
@RestController("SchResourceAuditController")
public class ResourceAuditController extends YxyApiBaseController {

    @Autowired
    private YxyResourceService yrs;

    @Autowired
    private YxyResourceServicePointService yxyResourceServicePointService;

    @ApiOperation(value = "学校审核资源")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/sch/platform/resource/audit", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String userId = getProfile(request, response).getId();
        String userNick = getProfile(request, response).getUserNick();
        String schoolId = getProfile(request, response).getSchoolId();
        String schoolName = getProfile(request, response).getSchoolName();
        RespModel model = new RespModel();
        yrs.audit(reqModel.auditStatus, reqModel.ids, reqModel.content, userId, userNick, schoolId, schoolName);
        for (String resId : reqModel.ids) {
            EsResourceUpdateUtil.updateEsResoure(resId);
        }
        if (reqModel.auditStatus == 1) {
            yxyResourceServicePointService.audioPass(Arrays.asList(reqModel.ids));
        } else if (reqModel.auditStatus == 3) {
            yxyResourceServicePointService.downPublish(Arrays.asList(reqModel.ids));
        }
        model.setMessage("成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String[] ids;
        // 1：已通过 2：未通过 3：已下架
        public Integer auditStatus;

        // 审核理由
        public String content;

        @Override
        public void autoCheck() {
            Assert.isTrue(ids != null && ids.length > 0, "请指定要审核的资源");
            Assert.isTrue(auditStatus == 1 || auditStatus == 2 || auditStatus == 3, "发生错误，请刷新页面");
        }
    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
